import http from "./http";
import qs from "qs";
import { requestOnline, ResponseCode, dayjs } from "./baseUrl";
/**
 *  @func H5
 *  @param request 请求地址 例如：http://197.82.15.15:8088/request/...
 *  @param '/testIp'代表vue-cil中config，index.js中配置的代理
 */

/**
 * @func 验证码
 */

const statisticsDataVerification = `/Statistics/data/verification`;
export async function getStatisticsDataVerification({ code }) {
  const response = await http.get(
    `${requestOnline}${statisticsDataVerification}?${qs.stringify({ code })}`
  );
  if (ResponseCode.success !== response.data.code) {
    const message =
      (typeof response.data.msg === "string" && response.data.msg) || "error";
    throw new Error(message);
  }
  return response.data.data;
}

/**
 * @func 查询总数与趋势图数据
 */
const statisticsDataNearly = `/Statistics/data/nearly`;
export async function getStatisticsDataNearly({
  startTime = dayjs(dayjs().format("YYYY-MM-DD"))
    .subtract(1, "day")
    .valueOf(),
  endTime = dayjs(dayjs().format("YYYY-MM-DD")).valueOf(),
}) {
  try {
    const response = await http.get(
      `${requestOnline}${statisticsDataNearly}?${qs.stringify({
        startTime,
        endTime,
      })}`
    );
    if (ResponseCode.success !== response.data.code) {
      const message =
        (typeof response.data.msg === "string" && response.data.msg) || "error";
      throw new Error(message);
    }
    return [null, response.data.data];
  } catch (error) {
    return [
      (typeof error.message === "string" && error.message) || "error",
      null,
    ];
  }
}

/**
 * @func 查询详情数据
 */
const statisticsDataListH5Data = `/Statistics/data/listH5Data`;
export async function getStatisticsDataListH5Data({
  pageNo = 1,
  pageSize = 100,
  id = "",
  key = "",
  startTime = dayjs(dayjs().format("YYYY-MM-DD"))
    .subtract(1, "day")
    .valueOf(),
  endTime = dayjs(dayjs().format("YYYY-MM-DD")).valueOf(),
  type = 1,
}) {
  try {
    const response = await http.get(
      `${requestOnline}${statisticsDataListH5Data}?${qs.stringify({
        pageNo,
        pageSize,
        id,
        key,
        startTime,
        endTime,
        type,
      })}`
    );
    if (ResponseCode.success !== response.data.code) {
      const message =
        (typeof response.data.msg === "string" && response.data.msg) || "error";
      throw new Error(message);
    }
    return [null, response.data.data];
  } catch (error) {
    return [
      (typeof error.message === "string" && error.message) || "error",
      null,
    ];
  }
}
